﻿using B.S.Outsourcing.Domain;
using B.S.Outsourcing.ErrorCode;
using B.S.Outsourcing.Read.Api.Application.Command;
using B.S.Outsourcing.Read.Api.Application.Command.OutsourcingArrivalOrderModel;
using B.S.Outsourcing.Read.Api.Application.Command.OutsourcingMaterialReleaseOrderModel;
using B.S.Outsourcing.Read.Api.Application.Command.OutSourOrderModel;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace B.S.Outsourcing.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OutSourcController : ControllerBase
    {
        private readonly IMediator mediator;

        public OutSourcController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 查询生产商
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingModel>> GetOutSourc([FromQuery]QueryOutsourCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingModel>> GetOutSourID([FromQuery]GetOutSourIDCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询委外订单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingOrderModel>> QueryOutSourOrder([FromQuery]QueryOutsourOrderCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id查询数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingOrderModel>> GetOutsourOrderID([FromQuery]QueryOutsourOrderIDCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询委外到货单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingArrivalOrder>> QuertOutArrial([FromQuery]QueryArrivaCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 根据id查询委外到货单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingArrivalOrder>> QuertOutArrialID([FromQuery]QueryArrivaIDCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 委外发料单列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<OutsourcingMaterialReleaseOrder>> QueryOutSourRelease([FromQuery]QueryOutSourReleaseOrderAllCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

/// <summary>
/// 导出生产商
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet]
        public async Task<IActionResult> Daochu([FromQuery] List<int> Id)
        {
            var request = new GetOutSourIDCommand { Id = Id };
            var result = await GetOutSourID(request);

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("委外生产商列表");
            IRow row = sheet.CreateRow(0);


            //设置表头
            string[] heads = { "生产商名称", "简称", "状态", "地址", "邮政编码", "电话", "联系人", "手机号", "开户行", "地址电话", "银行账号", "税务登记号","备注" };
            for (int i = 0; i < heads.Length; i++)
                row.CreateCell(i).SetCellValue(heads[i]);

            int index = 1;
            foreach (var item in result.list)
            {
                IRow rows = sheet.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(item.Name);
                rows.CreateCell(1).SetCellValue(item.Abbreviation);
                rows.CreateCell(2).SetCellValue(item.Status?"启用":"停用");
                rows.CreateCell(3).SetCellValue(item.Address);
                rows.CreateCell(4).SetCellValue(item.PostalCode);
                rows.CreateCell(5).SetCellValue(item.Telephone);
                
                rows.CreateCell(6).SetCellValue(item.ContactPerson);
                rows.CreateCell(7).SetCellValue(item.Mobile);
                rows.CreateCell(8).SetCellValue(item.Bank);
                rows.CreateCell(9).SetCellValue(item.BankAddressPhone);
                rows.CreateCell(10).SetCellValue(item.AccountNumber);
                rows.CreateCell(11).SetCellValue(item.TaxId);
                rows.CreateCell(12).SetCellValue(item.Remarks);
            }
            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "委外生产商列表.xlsx");
        }

        /// <summary>
        /// 导出委外订单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Daochuweiwai([FromQuery] List<int> Id)
        {
            var request = new QueryOutsourOrderIDCommand { Id = Id };
            var result = await GetOutsourOrderID(request);

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("委外订单列表");
            IRow row = sheet.CreateRow(0);


            //设置表头
            string[] heads = { "单据编码", "单据日期", "创建人", "单据状态", "业务类型", "业务状态" };
            for (int i = 0; i < heads.Length; i++)
                row.CreateCell(i).SetCellValue(heads[i]);

            int index = 1;
            foreach (var item in result.list)
            {
                IRow rows = sheet.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(item.DocumentCode);
                rows.CreateCell(1).SetCellValue(item.DocumentDate);
                rows.CreateCell(2).SetCellValue(item.CreatedBy);
                rows.CreateCell(3).SetCellValue(item.DocumentStatus);
                rows.CreateCell(4).SetCellValue(item.BusinessType);
                rows.CreateCell(5).SetCellValue(item.BusinessStatus);
            }
            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "委外生产商列表.xlsx");
        }

        /// <summary>
        /// 导出委外到货单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Daochudaohuo([FromQuery] List<int> Id)
        {
            var request = new QueryArrivaIDCommand { Id = Id };
            var result = await QuertOutArrialID(request);

            using IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("委外到货单列表");
            IRow row = sheet.CreateRow(0);


            //设置表头
            string[] heads = { "跟单编号", "订单交期", "单据编码", "单据状态", "仓库", "计划单号", "供应商", "物料名称", "物料编码", "到货数量", "单据日期" };
            for (int i = 0; i < heads.Length; i++)
                row.CreateCell(i).SetCellValue(heads[i]);

            int index = 1;
            foreach (var item in result.list)
            {
                IRow rows = sheet.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(item.FollowOrderNumber);
                rows.CreateCell(1).SetCellValue(item.OrderDeliveryDate);
                rows.CreateCell(2).SetCellValue(item.DocumentCode);
                rows.CreateCell(3).SetCellValue(item.DocumentStatus);
                rows.CreateCell(4).SetCellValue(item.Warehouse);
                rows.CreateCell(5).SetCellValue(item.PlanOrderNumber);
                rows.CreateCell(6).SetCellValue(item.Supplier);
                rows.CreateCell(7).SetCellValue(item.MaterialName);
                rows.CreateCell(8).SetCellValue(item.MaterialCode);
                rows.CreateCell(9).SetCellValue(item.ArrivalQuantity);
                rows.CreateCell(10).SetCellValue(item.DocumentDate);
            }
            using var ms = new MemoryStream();
            book.Write(ms);
            byte[] buffer = ms.ToArray();

            return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "委外到货单列表.xlsx");
        }
    }
}
